package server

import (
  "database/sql"
)

const (
  strSqlValidUser       = "SELECT `id` FROM `user` WHERE `name`=? AND `password`=? LIMIT 1"
  strSqlValidEmailExist = "SELECT `id` FROM `user` WHERE `email`=? LIMIT 1"
  strSqlValidUserExist  = "SELECT `id` FROM `user` WHERE `name`=? LIMIT 1"
)

func (app *App) dbValidUser(name, password string) (bool, error) {
  var id int64 = 0
  var err = app.db.QueryRow(strSqlValidUser, name, password).Scan(&id)
  if err != nil {
    return false, err
  }
  return (id > 0), nil
}

func (app *App) dbIsUserExist(name string) (bool, error) {
  var id int64 = 0
  var err = app.db.QueryRow(strSqlValidUserExist, name).Scan(&id)
  if err != sql.ErrNoRows && err != nil {
    return false, err
  }
  return (id > 0), nil
}

func (app *App) dbIsEmailExist(email string) (bool, error) {
  var id int64 = 0
  var err = app.db.QueryRow(strSqlValidEmailExist, email).Scan(&id)
  if err != sql.ErrNoRows && err != nil {
    return false, err
  }
  return (id > 0), nil
}
